﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data;

namespace DnDDB
{
    public class Weapon : Equipment {
    	
	    // non-derived variables
	    // non derived variables will have accessors and mutators
	    // and add method as well

    	
	    string damage; // damage is of the form "23d42 [+ C]", where 23, 42 and C are
				       // any integer
	    int range;     // 0 if not ranged weapon
        int crit_min;
		int crit_mult;
	    string type; 

	    public Weapon( string name ) : base(name){
            getMySQL get = new getMySQL();
			string dbCommand ="SELECT * FROM `Weapon` w, `Equipment_is_Weapon` eiw, `Equipment` e WHERE " +
            "e.name = '" + name + "' AND eiw.Equipment_id = e.id AND eiw.Weapon_id;";
        	DataRow dt = get.GetMySQLFilledRow(dbCommand);
			this.weight = Convert.ToInt32(dt["weight"]);
            this.crit_min = Convert.ToInt32(dt["crit_minimum"]);
            this.crit_mult = Convert.ToInt32(dt["crit_multiplier"]);
			this.type = dt["type"].ToString();
			this.range = Convert.ToInt32(dt["range"]);
            this.damage = dt["damage"].ToString();
	    }
	
		public Weapon( string name, bool equipped ) : base(name, equipped){
            getMySQL get = new getMySQL();
            string dbCommand = "SELECT * FROM `Weapon` w, `Equipment_is_Weapon` eiw, `Equipment` e WHERE " +
            "e.name = '" + name + "' AND eiw.Equipment_id = e.id AND eiw.Weapon_id;";
        	DataRow dt = get.GetMySQLFilledRow(dbCommand);
			this.weight = Convert.ToInt32(dt["weight"]);
            this.crit_min = Convert.ToInt32(dt["crit_minimum"]);
            this.crit_mult = Convert.ToInt32(dt["crit_multiplier"]);
			this.type = dt["type"].ToString();
			this.range = Convert.ToInt32(dt["range"]);
            this.damage = dt["damage"].ToString();
	    }
    	
	 
		    // jeff - check this thing's syntax!
            //Syntax checks out, but your for loop was missing a semicolon. You used a comma instead -Jeff
		    public object getDamage(bool getDamageByInteger){
                if (getDamageByInteger)
                {
                    int constantBonus = 0;
                    string diceRoll = damage;
                    if (damage.IndexOf("+") != -1)
                    {
                        constantBonus += Int32.Parse(damage.Split('+')[1]);
                        diceRoll = damage.Split('+')[0];
                    }
                    int numRolls = Int32.Parse(diceRoll.Split('d')[0]);
                    int die = Int32.Parse(diceRoll.Split('d')[1]);
                    int dam = constantBonus;

                    for (int i = 0; i < numRolls; i++)
                    {
                        Random rand = new Random();
                        dam += rand.Next(die);
                    }
                    return dam;
                }
                else
                    return damage;
		    }
    		
    		
		    public int getRange(){
			    return range;
		    }
		    public int getCritMultiplier(){
			    return crit_mult;
		    }
			
		 	public int getCritMinimum(){
				return crit_min;
			}
		    public string getType(){
			    return type;
		    }
    		public string getDamageAsString(){
			    return damage;
		    }
    	
    }
}
